package com.cfks.WebviewDriverTest;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Toast;
import com.cfks.webviewdriver.WebviewDriver;

public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.activitymainButton1).setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		try {
			final WebviewDriver wvd = new WebviewDriver((WebView) findViewById(R.id.webView));
			wvd.get("https://www.baidu.com", new WebviewDriver.OnLoadFinishListener(){
					public void onLoadFinish() {
						runOnUiThread(new Runnable(){
								@Override
								public void run() {
									toast("onLoadFinish");
									try {
										wvd.getElementsByClassName("hellosjsj");
									} catch (Exception e) {
										alert(e.toString());
									}
								}
							});
					}
				});
		} catch (Exception e) {
			alert(e.toString());
		}
	}

	public void alert(String message) {
		AlertDialog dialog = new AlertDialog.Builder(this)
			.setTitle("Message")
			.setMessage(message)
			.setPositiveButton(android.R.string.ok, null)
			.create();
		dialog.show();
	}

	public void toast(String m) {
		Toast.makeText(getApplication(), m, Toast.LENGTH_SHORT).show();
	}
}
